package com.hmkj.admin.controller.operate;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.model.operate.AnnounceModel;
import com.hmkj.core.po.operate.Announce;
import com.hmkj.core.service.operate.AnnounceService;
import lombok.extern.log4j.Log4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 公告
 */
@Log4j
@Controller
@RequestMapping(BaseController.BASE_URI)
public class AnnounceJsonController extends GenericController<Integer, Announce> {

    @Resource
    private AnnounceService announceService;


    /**
     * 公告列表josn数据
     *
     * @param dtGridPager
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("announce/json/list")
    @RequiresPermissions("sys:announce:list")
    public DtGrid listJson(String dtGridPager) throws Exception {
        DtGrid dtgrid = dataAuthority(dtGridPager, null);
        return announceService.getDtGridList(dtgrid,AnnounceModel.class);
    }
    /**
     * 新增公告
     * @param announce
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("announce/json/add")
    @RequiresPermissions("sys:announce:save")
    public ResultEntity addJson(Announce announce) throws  Exception{
        String result = checkModel(announce);
        if(result!=null){
            throw new BussinessException(result);
        }
        announce.setStatus(Announce.STATUS.WAIT.code);
        announce.setDelFlag(Announce.DELFLAG.NORMAL.code);
        announce.setCreateTime(new Date());
        announceService.insert(announce);
        return ok();
    }

    private String checkModel(Announce announce){
        String result = null;
        if(!StringUtils.isNotBlank(announce.getAgentId())){
            result = "请选择代理商";
        }else if(!StringUtils.isNotBlank(announce.getTitle())){
            result = "标题不能为空";
        }else if(!StringUtils.isNotBlank(announce.getContent())){
            result = "公告内容不能为空";
        }
        return  result;
    }

    /**
     * 编辑公告
     * @param announce
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("announce/json/edit")
    @RequiresPermissions("sys:announce:edit")
    public ResultEntity editJson(Announce announce) throws  Exception{
        String result = checkModel(announce);
        if(result!=null){
            throw new BussinessException(result);
        }
        announceService.updateByPrimaryKeySelective(announce);
        return ok();
    }


    /**
     * 发布
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "announce/json/publish")
    @RequiresPermissions("sys:announce:publish")
    public ResultEntity publish(Integer id) throws Exception {
        if(id==null){
            throw new BussinessException("参数有误");
        }
        Announce announce = announceService.selectOne(new Announce(a -> {
            a.setId(id);
            a.setDelFlag(Announce.DELFLAG.NORMAL.code);
        }));
        if (null == announce) {
            throw new BussinessException("您要发布的内容不存在或已被删除");
        } else if (announce.getStatus() == Announce.STATUS.PUBLISHED.code) {
            throw new BussinessException("当前状态下不能操作");
        }
        announceService.updateByPrimaryKeySelective(new Announce(a -> {
            a.setId(announce.getId());
            a.setStatus(Announce.STATUS.PUBLISHED.code);
            a.setReleaseTime(new Date());
        }));
        return ok();
    }

    /**
     * 撤回
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "announce/json/back")
    @RequiresPermissions("sys:announce:back")
    public ResultEntity back(Integer id) throws Exception {
        if(id==null){
            throw new BussinessException("参数有误");
        }
        Announce announce = announceService.selectOne(new Announce(a -> {
            a.setId(id);
            a.setDelFlag(Announce.DELFLAG.NORMAL.code);
        }));
        if (null == announce) {
            throw new BussinessException("您要撤回的内容不存在或已被删除");
        } else if (announce.getStatus() != Announce.STATUS.PUBLISHED.code) {
            throw new BussinessException("当前状态下不能操作");
        }
        announceService.updateByPrimaryKeySelective(new Announce(a -> {
            a.setId(announce.getId());
            a.setStatus(Announce.STATUS.BACK.code);
        }));
        return ok();
    }

    /**
     * 删除
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "announce/json/del")
    @RequiresPermissions("sys:announce:del")
    public ResultEntity del(Integer id) throws Exception {
        if(id==null){
            throw new BussinessException("参数有误");
        }
        Announce announce = announceService.selectOne(new Announce(a -> {
            a.setId(id);
            a.setDelFlag(Announce.DELFLAG.NORMAL.code);
        }));
        if (null == announce) {
            throw new BussinessException("您要删除的内容不存在或已被删除");
        } else if (announce.getStatus() == Announce.STATUS.PUBLISHED.code) {
            throw new BussinessException("当前状态下不能操作");
        }
        announceService.updateByPrimaryKeySelective(new Announce(a -> {
            a.setId(announce.getId());
            a.setDelFlag(Announce.DELFLAG.DELETED.code);
        }));
        return ok();
    }
}
